﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Reflection;
using Kawanon.Usability;
using Kawanon.QtHoney;
using QTPlugin;

namespace Kawanon.Usability.Actions
{
	public class Unknown : Action
	{
		public override string Name { get { return "不明"; } }

		public override void Execute(Arguments args)
		{
			throw new NotImplementedException();
		}
	}

	public class Pending : Action
	{
		public override string Name { get { return "未確定"; } }

		public override void Execute(Arguments args)
		{
			throw new NotImplementedException();
		}
	}

	[Serializable]
	public class MergeWindows : Action
	{
		public override string Name { get { return "ウィンドウをマージ"; } }

		public override void Execute(Arguments args)
		{
			//if (args.Server == null)
			//{
			//    DebugHelpers.Abort("Server == null");
			//}
			object tabBar = Utility.ReflectGetFieldValue(args.Server, "tabBar");
			if (tabBar == null)
			{
				Dbg.Abort("tabBar == null");
			}

			MethodInfo method = tabBar.GetType().GetMethod("MergeAllWindows",
				BindingFlags.Instance | BindingFlags.NonPublic, null, new Type[0], null);
			if (method == null)
			{
				Dbg.Abort("method == null");
			}
			object ret = method.Invoke(tabBar, new Object[0]);

			//DebugHelpers.Echo(ret);
		}
	}

	[Serializable]
	public class ShowGestureSettings : Action
	{
		public override string Name { get { return "マウスジェスチャの設定"; } }

		public override void Execute(Arguments args)
		{
			Utility.ShowForm(args.Server, new GestureForm(args.Server));
		}
	}

	[Serializable]
	public class NextTab : Action
	{
		public override string Name { get { return "次のタブへ"; } }

		public override void Execute(Arguments args)
		{
			if (args.Server.SelectedTab == null)
			{
				return;
			}
			ITab[] tabs = args.Server.GetTabs();

			int index = args.Server.SelectedTab.Index;
			index++;
			if (index >= tabs.Length)
			{
				index = 0;
			}
			args.Server.SelectedTab = tabs[index];
		}
	}

	[Serializable]
	public class PreviousTab : Action
	{
		public override string Name { get { return "前のタブへ"; } }

		public override void Execute(Arguments args)
		{
			if (args.Server.SelectedTab == null)
			{
				return;
			}
			ITab[] tabs = args.Server.GetTabs();

			int index = args.Server.SelectedTab.Index;
			index--;
			if (index < 0)
			{
				index = tabs.Length - 1;
			}
			args.Server.SelectedTab = tabs[index];
		}
	}

	[Serializable]
	public class IsolateTab : Action
	{
		public override string Name { get { return "タブを新しいウィンドウに分離"; } }

		public override void Execute(Arguments args)
		{
			if (args.Server.SelectedTab == null)
			{
				return;
			}

			ITab tab = args.Server.SelectedTab;
			Address addr = tab.Address;
			tab.Close();

			args.Server.CreateWindow(addr);
		}
	}

}
